<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>私有属性和方法 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    
    <link rel="prev" href="./chapter07.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="8"
        data-chapter-title="私有属性和方法"
        data-filepath="chapter08.md"
        data-basepath="."
        data-revision="Wed Feb 19 2020 00:43:01 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        类和对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        面向对象基本语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        魔法方法1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        魔法方法2
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        内置属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="./chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        类属性和对象属性
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="8" data-path="chapter08.html">
            
                
                    <a href="./chapter08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        私有属性和方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;">&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</h2>
<p>&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x5BF9;&#x8C61;&#x7684;&#x67D0;&#x4E9B;&#x5C5E;&#x6027;&#x6216;&#x8005;&#x65B9;&#x6CD5;&#x53EF;&#x80FD;&#x53EA;&#x5E0C;&#x671B;&#x5728;&#x5BF9;&#x8C61;&#x7684;&#x5185;&#x90E8;&#x522B;&#x4F7F;&#x7528;&#xFF0C;&#x800C;&#x4E0D;&#x5E0C;&#x671B;&#x5728;&#x5916;&#x90E8;&#x88AB;&#x8BBF;&#x95EE;&#x5230;&#xFF0C;&#x8FD9;&#x65F6;&#x5C31;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;<span style="color:red">&#x79C1;&#x6709;&#x5C5E;&#x6027;</span>&#x548C;<span style="color:red">&#x79C1;&#x6709;&#x65B9;&#x6CD5;</span>&#x3002;</p>
<h3 id="&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;">&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;</h3>
<p>&#x5728;&#x5B9A;&#x4E49;&#x5C5E;&#x6027;&#x6216;&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x5728;&#x5C5E;&#x6027;&#x540D;&#x6216;&#x8005;&#x65B9;&#x6CD5;&#x540D;&#x524D;&#x589E;&#x52A0;&#x4E24;&#x4E2A;&#x4E0B;&#x5212;&#x7EBF;<code>__</code>&#xFF0C;&#x5B9A;&#x4E49;&#x7684;&#x5C31;&#x662F;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x6216;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,age)</span>:</span>
        self.name = name
        self.age = age
        self.__money = <span class="hljs-number">2000</span>  <span class="hljs-comment"># &#x4F7F;&#x7528; __ &#x4FEE;&#x9970;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x662F;&#x79C1;&#x6709;&#x5C5E;&#x6027;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__shopping</span><span class="hljs-params">(self, cost)</span>:</span>
        self.__money -= cost  <span class="hljs-comment"># __money &#x53EA;&#x80FD;&#x5728;&#x5BF9;&#x8C61;&#x5185;&#x90E8;&#x4F7F;&#x7528;</span>
        print(<span class="hljs-string">&apos;&#x8FD8;&#x5269;&#x4E0B;%d&#x5143;&apos;</span>%self.__money)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">(self)</span>:</span>
        self.__shopping(<span class="hljs-number">200</span>)  <span class="hljs-comment"># __shopping &#x65B9;&#x6CD5;&#x4E5F;&#x53EA;&#x80FD;&#x5728;&#x5BF9;&#x8C61;&#x5185;&#x90E8;&#x4F7F;&#x7528;</span>

p = Person(<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>,<span class="hljs-number">18</span>)
<span class="hljs-comment"># print(p.__money)   &#x8FD9;&#x91CC;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x5BF9;&#x8C61;&#x5185;&#x90E8;&#x7684;&#x79C1;&#x6709;&#x5C5E;&#x6027;</span>
p.test()
<span class="hljs-comment"># p.__shopping()  &#x8FD9;&#x91CC;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;__shopping &#x53EA;&#x80FD;&#x5728;&#x5BF9;&#x8C61;&#x5185;&#x90E8;&#x4F7F;&#x7528;&#xFF0C;&#x5916;&#x90E8;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE;</span>
</code></pre>
<h3 id="&#x8BBF;&#x95EE;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;">&#x8BBF;&#x95EE;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;</h3>
<p>&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#xFF0C;&#x79C1;&#x6709;&#x65B9;&#x6CD5;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x901A;&#x8FC7;&#x4E00;&#x4E9B;&#x4EE3;&#x7801;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;&#x5916;&#x90E8;&#x8BBF;&#x95EE;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;</p>
<h4 id="&#x76F4;&#x63A5;&#x8BBF;&#x95EE;">&#x76F4;&#x63A5;&#x8BBF;&#x95EE;</h4>
<p>&#x4F7F;&#x7528;&#x65B9;&#x5F0F;:&#x5728;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x540D;&#x6216;&#x65B9;&#x6CD5;&#x540D;&#x524D;&#x6DFB;&#x52A0; <code>_&#x7C7B;&#x540D;</code></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,age)</span>:</span>
        self.name = name
        self.age = age
        self.__money = <span class="hljs-number">2000</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__shopping</span><span class="hljs-params">(self, cost)</span>:</span>
        self.__money -= cost


p = Person(<span class="hljs-string">&apos;&#x674E;&#x56DB;&apos;</span>,<span class="hljs-number">20</span>)
print(p._Person__money)  <span class="hljs-comment"># &#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x540D;._&#x7C7B;&#x540D;__&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x540D; &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8BBF;&#x95EE;&#x5BF9;&#x8C61;&#x7684;&#x79C1;&#x6709;&#x5C5E;&#x6027;</span>
p._Person__shopping(<span class="hljs-number">100</span>)  <span class="hljs-comment"># &#x4F7F;&#x7528;&#x5BF9;&#x8C61;&#x540D;._&#x7C7B;&#x540D;__&#x51FD;&#x6570;&#x540D; &#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x5BF9;&#x8C61;&#x7684;&#x79C1;&#x6709;&#x65B9;&#x6CD5;</span>
print(p._Person__money)
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;<span style="color:red">&#x5F3A;&#x70C8;&#x4E0D;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528; <code>&#x5BF9;&#x8C61;&#x540D;._&#x7C7B;&#x540D;__&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x540D;</code> &#x7684;&#x65B9;&#x5F0F;&#x6765;&#x8BBF;&#x95EE;&#x5BF9;&#x8C61;&#x7684;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#xFF01;</span></p>
<h4 id="&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;&#x8BBF;&#x95EE;&#x79C1;&#x6709;&#x53D8;&#x91CF;">&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;&#x8BBF;&#x95EE;&#x79C1;&#x6709;&#x53D8;&#x91CF;</h4>
<p>&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x5BF9;&#x8C61;&#x7684;&#x53D8;&#x91CF;&#x4F7F;&#x7528;&#x4E86;<code>__</code> &#x6765;&#x4FEE;&#x9970;&#xFF0C;&#x5C31;&#x8BF4;&#x660E;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x79C1;&#x6709;&#x53D8;&#x91CF;&#xFF0C;&#x4E0D;&#x5EFA;&#x8BAE;&#x5916;&#x90E8;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x548C;&#x4FEE;&#x6539;&#x3002;&#x5982;&#x679C;&#x786C;&#x8981;&#x4FEE;&#x6539;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B9A;&#x4E49;<code>get</code>&#x548C;<code>set</code>&#x65B9;&#x6CD5;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x6765;&#x5B9E;&#x73B0;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self,name,age)</span>:</span>
        self.name = name
        self.age = age
        self.__money = <span class="hljs-number">2000</span>  <span class="hljs-comment"># __money &#x662F;&#x79C1;&#x6709;&#x53D8;&#x91CF;&#xFF0C;&#x5916;&#x90E8;&#x65E0;&#x6CD5;&#x8BBF;&#x95EE;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_money</span><span class="hljs-params">(self)</span>:</span>  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x4E86;get_money &#x65B9;&#x6CD5;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x91CC;&#x83B7;&#x53D6;&#x5230; __money</span>
        <span class="hljs-keyword">return</span> self.__money  <span class="hljs-comment"># &#x5185;&#x90E8;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE; __money &#x53D8;&#x91CF;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">set_money</span><span class="hljs-params">(self,money)</span>:</span> <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x4E86;set_money &#x65B9;&#x6CD5;&#xFF0C;&#x5728;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x91CC;&#xFF0C;&#x53EF;&#x4EE5;&#x4FEE;&#x6539; __money</span>
        self.__money = money

p = Person(<span class="hljs-string">&apos;&#x738B;&#x4E94;&apos;</span>,<span class="hljs-number">21</span>)

<span class="hljs-comment"># &#x5916;&#x90E8;&#x901A;&#x8FC7;&#x8C03;&#x7528; get_money &#x548C; set_money &#x8FD9;&#x4E24;&#x4E2A;&#x516C;&#x5F00;&#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x548C;&#x4FEE;&#x6539;&#x79C1;&#x6709;&#x53D8;&#x91CF;</span>
print(p.get_money())
p.set_money(<span class="hljs-number">8000</span>)
print(p.get_money())
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter07.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 类属性和对象属性"><i class="fa fa-angle-left"></i></a>
        
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
